#include "stdafx.h"
#include "MyModel.h"


MyModel::MyModel(void) : mLastMouseX( 0 ) , mLastMouseY( 0 ) , mState( eCONNECTING )
	, mIoLoop(), mClient(mIoLoop, this), mPacketProducer(mClient)
{
	// TODO : Socket connection needs to be here
	// new socket . connect( 127.0.0.1 ); something like this
	// If connection completed , call SetState( eWAITINGFORMATCHING ) till find matching.
	// After Matching , call SetState( eGAMEPLAYING ) 

	mClient.Start(std::string("127.0.0.1"), std::string("9000"));

	unsigned __int64 userId = 1234567890;

	mPacketProducer.Login(userId);
}

MyModel::~MyModel(void)
{
	mClient.Stop();
}

void MyModel::OnStarted()
{
	// on_connected
}

void MyModel::OnStopped()
{
	// on_disconnected
}

void MyModel::OnLogin(bool result)
{

}

void MyModel::DoLogic()
{
	// Oliver commented
	//this->packetParser();
}

// Oliver commented
//void MyModel::packetParser()
//{
//	// TODO : Packet Incoming check here
//	// Peek Packet queue and if there something , parse it and call OnPacket.
//}

void MyModel::OnMouseMove	( float fX , float fY )
{
	mLastMouseX = fX;
	mLastMouseY = fY;
}

void MyModel::OnLButtonDown	( float fX , float fY )
{
	this->SetState( eWAITINGFORMATCHING );

	TCHAR str[ 256 ];
	swprintf( str , 256 , _T( "( %.0f , %.0f )" ) , fX , fY );

	this->Trace( std::wstring( str ) );
}

// Oliver commented
//void MyModel::OnPacket()
//{
//	// TODO : Packet handler
//	// need packet type and parameters.
//	// long switch cases.
//}

void MyModel::SetState( GameState inState )
{
	if( this->mState != inState )
	{
		this->mState = inState;

		switch( this->mState )
		{
		case eCONNECTING			:
			{
				// Never happens
				assert( !"Should not happen this" );
			}
			break;
		case eWAITINGFORMATCHING	:
			{
				// show waiting UI, and run ping from here.
			}
			break;
		case eGAMEPLAYING			:
			{
				// Initialize game objects and switch to game screen.
			}
			break;
		case eDISCONNECTED			:
			{
				// close the connection and show error message.
			}
			break;

		}
	}
}